//JavaScript

var registerSession;
var typeSSwap;
var siteSwapList;
var curentSiteSwapList;
var nbPages;
var curentNbPages;
var curentNbObject;
var curentPage;
var curentType;
var nbSiteSwap;
var respSiteSwap;

function setRegisterSession(registerSessionToSet){
  this.registerSession = registerSessionToSet;
}

function getSiteSwaps(typeSiteSwap, startPos, nbObject, nbPage){
  respSiteSwap = false;
  curentNbObject = nbObject;
  curentPage = nbPage;
  typeSSwap = typeSiteSwap;
  curentType = typeSiteSwap;
  var url = 'include/bd/siteswap_bd.php';
  $.getJSON(url
        ,{typeSiteSwap:typeSiteSwap
          , start_pos:startPos
          , nb_object:nbObject}
        ,gestionReponseSiteSwaps);
  setTimeout(function(){
  if (!respSiteSwap){
      $('#siteSwaps').html("<DIV ALIGN='center'><IMG SRC='images/loading.gif' ALIGN='center'></DIV>");
  }
  },100);
}

function gestionReponseSiteSwaps(reponse)
{
    respSiteSwap = true;
    nbSiteSwap = reponse[0];
    var nbObjectArray = reponse[1];
    var siteSwaps = reponse[2];
    siteSwapList = siteSwaps;
    curentSiteSwapList = siteSwapList;
    if (siteSwapList){
      // Affichage du filte du nombre d'objets
      afficheNbObjects(nbObjectArray);
      if (siteSwapList.length > 0)
      {
          // affichage des siteswaps
          nbPages = Math.ceil(nbSiteSwap / 5);
          curentNbPages = nbPages;
          afficheEnteteSiteSwaps();
          var limit = Math.min(siteSwaps.length, 5);
          var initValueCompt = 0;
          afficheFourchetteSiteSwaps(initValueCompt, limit);
      }
    }
}

function afficheNbObjects(nbObjectArray){
nbObjectArray.sort();

//CREATION DU SQUELETE HTML
var html = ""+
"<TABLE >"+
	"<TD >Nombre d'objets </TD>"+
	"<TD WIDTH='10px' />"+
	"<TD align='left'>"+
	"<SELECT ID='idNbObject' onChange='javascript:filtreSiteSwaps(this.value);'  name='nbObject'>"+
		"<OPTION value='*'>Tous</OPTION>";
		// INJECTATION DES DONNEES
    for (var i=0; i < nbObjectArray.length; i++){
      var nbObject = nbObjectArray[i];
      html += "<OPTION value='"+nbObject+"'>"+nbObject+"</OPTION>";
    }
    // Poursuite du squelette HTML
		html += ""+		
	"</SELECT>"+
	"</TD>"+
"</TABLE>";

$('#nbObject').html(html);

// On met Ó jour la combo en fonction de l'id sÚlectionnÚ
  var optionList = $('#idNbObject')[0].options;
  for (var i =0; i < optionList.length; i++){
    var option = optionList[i];
    if (option.value == curentNbObject){
      option.selected = true;
      break;
    }
  }
}

function afficheEnteteSiteSwaps(){
   var html = "";
  if (curentNbPages > 1){
    //CREATION DU SQUELETE HTML
    html = ""+
    "<TABLE WIDTH='95%'>"+
    	"<TD align='right'>"+
    	"Aller à la page";
    	// INJECTATION DES DONNEES
    		for (var i=0 ; i < curentNbPages ; i++)
        {
          if (i != 0){
            html += ", ";
          }else{
            html += " ";
          }
          if (i == curentPage){
            html += (i+1);
          }else{
            html += "<A class='blanc' HREF='javascript:changePage("+(i)+");'>"+(i+1)+"</A>";
          }  
        }
      // Poursuite du squelette HTML
      html += ""+
    "</TD>"+
    "</TABLE>";
  }
  $('#enteteSiteSwaps').html(html);
}

function afficheFourchetteSiteSwaps(initComptValue, maxComptValue){
  var str = "";
  for (var i=initComptValue ; i < maxComptValue ; i++){
      var siteSwap = curentSiteSwapList[i];
      str += afficheSiteSwap(siteSwap);
  }
  $('#siteSwaps').html(str);
}


function afficheSiteSwap(siteSwap){
//CREATION DU SQUELETE HTML
var html = ""+
"<DIV id='siteswap_top'>"+
  "<TABLE WIDTH='800px'>"+
    "<TR>"+
      "<TD  style=' align:right; width:700px; color:white;' >"+
        "<A HREF='javascript:filtreSiteSwaps("+siteSwap.nbObject+");' class='categorie'>"+siteSwap.nbObject+" Objets </A> "+
				"<A NAME='"+siteSwap.idSiteSwap+"' class='categorie'></A>";
				if (siteSwap.titre != ""){
				  html += ""+
          ": <B>"+html_entity_decode(siteSwap.titre)+"</B>";
        }
        html += ""+
        ": code sitesswap <B>"+siteSwap.codeSiteSwap+"</B>"+
      "</TD>"+
    "</TR>"+
  "</TABLE>"+
"</DIV>"+
"<DIV id='siteswap_content_back'>"+
  "<DIV id='siteswap_content'>"+
			"<TABLE>"+
				"<TD valign='top'  WIDTH='300px'><DIV>"+html_entity_decode(siteSwap.texte)+"<br><br></DIV>"+				  
          "<DIV ID='idShowSSwap"+siteSwap.idSiteSwap+"'>"+
					   "Siteswap : 	<A HREF='javascript:showSiteSwap("+siteSwap.idSiteSwap+");'>"+siteSwap.codeSiteSwap+"</A> "+
				  "</DIV>"+
				"</TD>"+		
				"<TD  WIDTH='480px'>"+
				"<DIV ID='idSSwap"+siteSwap.idSiteSwap+"'>"+				
				  "</DIV>"+
				"</TD>"+
			"</TABLE>"+
	"</DIV>"+
"</DIV>"+
"<DIV id='siteswap_content_end' />";
if (registerSession == 1) {
 html += ""+
 "<DIV id='siteswap_bottom'>"+
  "<A class='blanc' HREF='index.php?page=update_siteswap&type_sswap="+typeSSwap+"&id_sswap="+siteSwap.idSiteSwap+"'>Editer ce siteswap</A> -"+
 "</DIV>";
}
html += ""+		
"<BR><BR>";

return html;
}

function changePage(numPage){
  var initValueCompt = (numPage * 5) ;
  getSiteSwaps(curentType, initValueCompt, curentNbObject, numPage);
}

function showSiteSwap(idSiteSwap){
  var siteSwap = searchSiteSwap(idSiteSwap);
  var baliseSiteSwap = "#idSSwap"+idSiteSwap;
  var baliseShowSiteSwap = "#idShowSSwap"+idSiteSwap;
  $(baliseSiteSwap).hide();
  html = ""+
  "<DIV>"+
    "<iframe width='480' height='360' frameborder='0' scrolling='no' marginheight='0' marginwidth='0'"+ 
              "src=''>"+
    "</iframe>"
  "</DIV>"+
  "<DIV>"+
     "<A HREF='javascript:hideSiteSwap("+idSiteSwap+");'>Cacher le siteswap</A>"+
  "</DIV> ";
  $(baliseSiteSwap).html(html);
  
  $(baliseSiteSwap).show(4000, function(){
    $(baliseShowSiteSwap).hide("slow");
    
     html = ""+
      "<DIV>"+
        "<iframe ID='"+baliseSiteSwap+"Frame' width='480' height='360' frameborder='0' scrolling='no' marginheight='0' marginwidth='0'"+ 
                "src='http://jugglinglab.sourceforge.net/siteswap.php?"+siteSwap.codeSiteSwapPhp+"'>"+
        "</iframe>"+
      "</DIV>"+
      "<DIV>"+
        "<A HREF='javascript:hideSiteSwap("+idSiteSwap+");'>Cacher le siteswap</A>"+
      "</DIV> ";
    $(baliseSiteSwap).html(html);
     
  });
  
}

function hideSiteSwap(idSiteSwap){
  var siteSwap = searchSiteSwap(idSiteSwap);
  var baliseSiteSwap = "#idSSwap"+idSiteSwap;
  var baliseShowSiteSwap = "#idShowSSwap"+idSiteSwap;
  var html = ""+
  "<DIV>"+
        "<iframe ID='"+baliseSiteSwap+"Frame' width='480' height='360' frameborder='0' scrolling='no' marginheight='0' marginwidth='0'"+ 
                "src=''>"+
        "</iframe>"+
      "</DIV>"+
      "<DIV>"+
        "<A HREF='javascript:hideSiteSwap("+idSiteSwap+");'>Cacher le siteswap</A>"+
      "</DIV> ";
  $(baliseSiteSwap).html(html);  
  $(baliseSiteSwap).hide(4000);
  $(baliseShowSiteSwap).show(4000);
}

function searchSiteSwap(idSiteSwap){
  var siteSwap = null; 
  for (var i = 0; i < curentSiteSwapList.length; i++){
    var siteSwapTemp = curentSiteSwapList[i];
    if (siteSwapTemp.idSiteSwap == idSiteSwap){
      siteSwap = siteSwapTemp;
      break;
    }
  }
  return siteSwap;
}

function filtreSiteSwaps(nbObject){

  getSiteSwaps(curentType, 0, nbObject,0);
    
}
